﻿using HP.UtilsLib.Devices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VoiceAI;
using VoiceAI.VoiceResponse;
using Windows.Foundation.Collections;
using Xiaowei.Models;
using Xiaowei.Services;

namespace Xiaowei.ResponseHandlers
{
    internal static class PCControlResponseHandler
    {
        private static readonly IVolumeControl volumeControl = new VolumeControl();
        public static void Handle(IResponseData responseData)
        {
            if(responseData is IPCControlResponseData pcControlResponseData)
            {
                if (//SystemState.IsLocked
                    //&& NotSupportInLockMode.ContainsKey(pcControlResponseData.Type)
                    false)
                {
                    responseData.ResponseText = NotSupportInLockMode[pcControlResponseData.Type];
                    responseData.TTSUrl = XiaoweiAI.Instance.RequestTTS(NotSupportInLockMode[pcControlResponseData.Type]);
                }
                else
                {
                    switch (pcControlResponseData.Type)
                    {
                        case PCControlType.AddVolume:
                            AddVolume(pcControlResponseData.Value);
                            break;
                        case PCControlType.SetVolumeTo:
                            SetVolume(pcControlResponseData.Value);
                            break;
                        case PCControlType.MuteSpeaker:
                            SetMute(true);
                            break;
                        case PCControlType.UnmuteSpeaker:
                            SetMute(false);
                            break;
                        case PCControlType.AddBrightness:
                            AddBrightness(pcControlResponseData.Value);
                            break;
                        case PCControlType.SetBrightnessTo:
                            SetBrightnessTo(pcControlResponseData.Value);
                            break;
                        case PCControlType.Shutdown:
                            Shutdown();
                            break;
                        case PCControlType.Lockscreen:
                            Lockscreen();
                            break;
                        default:
                            break;
                    }
                }
            }
            DefaultResponseHandler.Handle(responseData);
        }
        private static Dictionary<PCControlType, string> NotSupportInLockMode { get; } = new Dictionary<PCControlType, string>()
        {
            {PCControlType.Shutdown,CommonReplyModel.NotSupport },
            {PCControlType.Lockscreen,CommonReplyModel.AlreadyLocked },
        };
        //watch skills that this model likes

        //watch commands that this model likes
        private static void AddVolume(double volume)
        {
            if (volume == -2) volume = -0.1;
            if (volume == 2) volume = 0.1;
            if (volume > 1 || volume < -1) return;
            double nowVolume = volumeControl.GetVolume();
            volumeControl.SetVolume(nowVolume + volume);
        }

        private static void SetVolume(double volume)
        {
            if (volume > 1 || volume < -1) return;
            volumeControl.SetVolume(volume);
        }

        private static void SetMute(bool isMute)
        {
            volumeControl.SetMute(isMute);
        }

        private static void AddBrightness(double brightness)
        {
            if (brightness == -2) brightness = -0.1;
            if (brightness == 2) brightness = 0.1;
            if (brightness > 1 || brightness < -1) return;
            var b = Display.Instance.GetBrightness();
            if (b.HasValue)
            {
                Display.Instance.SetBrightness(b.Value + brightness);
            }
        }

        private static void SetBrightnessTo(double brightness)
        {
            Display.Instance.SetBrightness(brightness);
        }
        static IPCControlService PCControlService => ServiceFactory.PCControlService;
        private static void Shutdown()
        {
            PCControlService.ShutDown();
        }
        private static void Lockscreen()
        {
            PCControlService.LockScreen();
        }
    }
}
